// pages/Goods/Comment/index.js
var common = require('../../../public.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemId: '',
    commnet: [],
    showComment: false,
    commentPlaceHolder: '我来说几句',
    commentValue: '',
    inputFocus: false,
    pageNum: 1,
    isloadingBusy: false,
    loadFinished: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '用户评论'
    })
    const that = this;
    this.setData({
      itemId: options.itemId
    })
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
          openid: res.data.openid
        })
        // 获取商品评论
        that.getGoodsCommentList(true)
      },
      fail: function (res) {
        common.showLoginPage()
      }
    })
  },
  //获取评论列表
  getGoodsCommentList(refresh = false) {
    if (this.data.isloadingBusy === true || this.data.loadFinished === true) {
      return
    }
    this.setData({
      isloadingBusy: true
    })
    // 加载更多，页码加1
    const pageNum = refresh ? 1 : this.data.pageNum + 1

    const that = this
    const tempJson = {
      itemId: that.data.itemId,
      pageNum: '' + pageNum,
      type: 1, //0标识取前2条   1全部数据
      userId: that.data.userInfo.id
    }
    common.post('/comment/getItemCommentlist', tempJson, function (res) {
      if (res.status === 200) {
        if (res.data.commnetList) {
          const tempList = [...that.data.commnet, ...res.data.commnetList]
          const finshed = res.data.commnetTotalNums == tempList.length ? true : false
          that.setData({
            commnet: tempList,
            isloadingBusy: false,
            loadFinished: finshed,
            pageNum
          })
        } else {
          that.setData({
            isloadingBusy: false,
            loadFinished: true
          })
        }
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  //点击回复
  clickRely(e) {
    let {
      item
    } = e.currentTarget.dataset
    let that = this
    console.log(item)
    this.setData({
      showComment: true,
      commentPlaceHolder: '@' + item.alias + ':',
      commentId: item.id
    }, function () {
      setTimeout(function () {
        that.setData({
          inputFocus: true
        })
      }, 500)
    })
  },
  //输入评论
  changeCommnet(e) {
    console.log(e)
    let {
      value
    } = e.detail
    this.setData({
      commentValue: value
    })
  },
  // 发表评论
  saveComment() {
    if (this.data.commentValue === null || this.data.commentValue.length === 0) {
      wx.showToast({
        icon: 'none',
        title: '您没有输入任何文字',
      })
      return
    }
    let that = this
    wx.cloud.callFunction({
      name: 'ContentCheck',
      data: {
        txt: that.data.commentValue
      },
      success(res) {
        if (res.result.errCode == 87014) {
          console.log(res.errCode)
          wx.showToast({
            icon: 'none',
            title: '文字违规，请重新输入',
          })
        } else {
          // 合规
          let tempJson = {
            userId: that.data.userInfo.id,
            token: that.data.token,
            commentId: that.data.commentId,
            content: that.data.commentValue
          }
          common.post('/comment/saveItemCommentChat', tempJson, function (res) {
            if (res.status == 200) {
              that.onCommentClose()
              that.getGoodsCommentList(true)
              common.showToast('评论成功')
            } else {
              common.showRequestErrMsg(res)
            }
          })
        }
      },
      fail(err) {
        wx.showToast({
          icon: 'none',
          title: '文字违规，请重新输入',
        })
      }
    })
  },
  onCommentClose() { // 关闭模态框
    this.setData({
      commentId: ' ',
      commentValue: ' ',
      showComment: false,
    })
  },
  commentPraise(e) { //点赞
    let {
      id
    } = e.currentTarget.dataset
    let that = this
    let tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token,
      commnetId: id
    }
    common.post('/comment/saveCommentPraise', tempJson, function (res) {
      if (res.status == 200) {
        that.getGoodsCommentList(true)
      } else {
        that.cancleCommentPraise(id)
      }
    })
  },
  //取消点赞
  cancleCommentPraise(id) {
    let that = this
    let tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token,
      commnetId: id
    }
    common.post('/comment/cancleCommentPraise', tempJson, function (res) {
      common.showToast('取消点赞')
      that.getGoodsCommentList(true)
    })
  },
  loadMore() {
    this.getGoodsCommentList(false)
  }
})